//
//  JZHAddressViewController.m
//  极致荟
//
//  Created by hetao on 14-8-22.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHAddressViewController.h"
#import "JZHAddressTableViewCell.h"
#import "JZHAddAdderssViewController.h"
#import "jzhAdders.h"
@interface JZHAddressViewController ()

@end

@implementation JZHAddressViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.table reloadData];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.table setTableFooterView:self.addview];
    [self.table setTableHeaderView:self.top];
    [self httpdata];
    //[self.table registerClass:[JZHAddressTableViewCell class] forCellReuseIdentifier:@"address"];
    self.navigationItem.title=@"送货地址";
    [self.table registerNib:[UINib nibWithNibName:@"JZHAddressTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"address"];
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
    [leftButton addTarget:self action:@selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    self.automaticallyAdjustsScrollViewInsets=NO;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)leftDrawerButtonPress:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [User defuceUser].address.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 72;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JZHAddressTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"address" forIndexPath:indexPath];
    [cell addCellData:[User defuceUser].address[indexPath.row] viewcontroller:self];
    return cell;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete;
}
-(NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath
{
    return @"删除";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    JZHAdders *addersv=[[User defuceUser].address objectAtIndex:indexPath.row];
    [[User defuceUser].address removeObjectAtIndex:indexPath.row];
  
    NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:[User defuceUser].userId],@"userId",addersv.addid,@"addressId", nil];
    [HttpRequest httpRequestPost:JZHHTTPURL(@"myInformation/deldetailAddress") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        
    }];
   [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  withRowAnimation: UITableViewRowAnimationNone];
}
- (IBAction)addadderss:(id)sender
{
    [self.navigationController pushViewController:[[JZHAddAdderssViewController alloc] init] animated:YES];
}

- (void)httpdata
{
    NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:[User defuceUser].userId],@"userId", nil];
    [HttpRequest httpRequestPost:JZHHTTPURL(@"myInformation/detailAddressList") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        if(rerurncode==0)
        {
            NSLog(@"%@",data);
            NSMutableArray *ta=[[NSMutableArray alloc] init];
            for(NSDictionary *datadic in [data objectForKey:@"data"])
            {
                JZHAdders *ae=[[JZHAdders alloc] init];
                ae.addid=[datadic objectForKey:@"id"];
                ae.name=[datadic objectForKey:@"consignee"];
                ae.addres=[datadic objectForKey:@"simpleAddress"];
                ae.content=[datadic objectForKey:@"detail_address"];
                ae.postCode=[datadic objectForKey:@"postcode"];
                ae.phone=[datadic objectForKey:@"telphone"];
                [ta addObject:ae];
            }
            [User defuceUser].address=ta;
            [self.table reloadData];
        }
    }];
}


@end
